using System;
using System.IO;
using System.Text.RegularExpressions;
using Drifter.Core;
using System.Collections.Generic;
using System.Collections;
using System.Text;

namespace Drifter.Datasource
{

    public class FileSource : IDataSource
    {
        public bool Active { get; set; }
        public ITokenizer Tokenizer { get; set; }
        StreamReader sr;

        /// <summary>
        /// Initializes a new instance of the <see cref="T:LargeTextfileReader"/> class.
        /// </summary>
        /// <param name="path">The path.</param>
        public FileSource(string path, Encoding encoding)
        {
            sr = new StreamReader(path, encoding);
        }


        /// <summary>
        /// Gets the enumerator.
        /// </summary>
        /// <returns></returns>
        public IEnumerator<FieldList> GetEnumerator()
        {
            while (sr.Peek() != -1)
            {
                string line = sr.ReadLine();
                // ############ !!!!!!!!!!!! archituktir --> Fieldlist parameter
                FieldList fieldList = Tokenizer.Tokenize("Main", null, line);
                yield return fieldList;
            }
            Dispose();
        }

        /// <summary>
        /// Returns an enumerator that iterates through a collection.
        /// </summary>
        /// <returns>
        /// An <see cref="T:System.Collections.IEnumerator"/> object that can be used to iterate through the collection.
        /// </returns>
        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }

        /// <summary>
        /// Releases unmanaged and - optionally - managed resources
        /// </summary>
        public void Dispose()
        {
            if (sr != null) sr.Close();
        }
    }
}


